#!/bin/bash
{% if iiab_wireless_lan_iface is undefined %}
echo -e "\nUH-OH: Your Wi-Fi hardware was not found'\n"
{% else %}
{% if not network_enabled %}
echo -e "Networking role disabled\n"
echo -e "If you want to enable it, check network_install and check network_enabled in /etc/iiab/local_vars.yml and then run: sudo iiab-network\n"
echo -e "For details, see: https://github.com/iiab/iiab/pull/3302\n"
{% else %}
sed -i "s/^HOSTAPD_ENABLED.*/HOSTAPD_ENABLED=False/" {{ iiab_env_file }}
systemctl disable hostapd
systemctl stop hostapd
{% if wifi_up_down %}
systemctl disable iiab-clone-wifi.service
systemctl disable iiab-wifi-test.service
systemctl stop iiab-clone-wifi.service
echo -e "\nIIAB hotspot (Wi-Fi access point) Disabled.\n"
#exit 0
{% else %}
echo -e "\nIIAB hotspot (Wi-Fi access point) Disabled.\n"
{% if dhcpcd_result == "enabled" %}
# hotspot-off before ap0_updown
sed -i "s/^denyinterfaces/#denyinterfaces/" /etc/dhcpcd.conf
#systemctl disable dnsmasq
#systemctl stop dnsmasq
systemctl daemon-reload
systemctl restart dhcpcd
#systemctl restart networking 6/15/2019 TFM removed
# Temporary promiscuous-mode workaround for RPi's WiFi "10SEC disease"
# Set wlan0 to promiscuous when AP's OFF (for possible WiFi gateway)
# SEE ALSO iiab-hotspot-on + /usr/libexec/iiab-startup.sh
# https://github.com/iiab/iiab/issues/638#issuecomment-355455454
#if grep -qi raspbian /etc/*release; then
#    ip link set dev wlan0 promisc on
#fi
{% else %}
#ubuntu (or Mint, or pure Debian?)
if [ -f /etc/NetworkManager/conf.d/wifi-manage.conf ]; then
    sed -i "s|managed=0|managed=1|" /etc/NetworkManager/conf.d/wifi-manage.conf
fi
echo -e "\nIf you're enabling upstream WiFi, please reboot now.\n"
#exit 0
{% endif %}
#if dhcpcd_result == "enabled"
{% endif %}
#wifi_up_down
{% endif %}
#network_enabled
{% endif %}
#Wifi not found
